﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.AuthModel;
using BLL.AuthModel;
namespace Com.Ompchina.Web.admin.system
{
    public partial class add_moduletype : AddInfoPage, IAddInfo
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && PageAction.Equals("update")) { this.InitSingleInfo(this.GetUpdateID()); }
        }

        #region IPageAdd 成员

        public bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                base.SetMessage(false, "请输入分类名称");
                return false;
            }
            else if (string.IsNullOrEmpty(txtDescription.Text.Trim()))
            {
                base.SetMessage(false, "请输入分类描述");
                return false;
            }
            return true;
        }

        public void InitSingleInfo(int id)
        {
            if (id > 0)
            {
                ModuleTypeInfo info = new ModuleTypeBLL().GetModuleTypeByID(id);
                if (info.ModuleTypeID > 0)
                {
                    txtName.Text = info.ModuleTypeName;
                    txtDescription.Text = Server.HtmlDecode(info.ModuleTypeDescription);
                    hfID.Value = id.ToString();
                }
            }
        }

        #endregion


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                ModuleTypeInfo info = new ModuleTypeInfo();
                info.ModuleTypeName = Server.HtmlEncode(txtName.Text.Trim());
                info.ModuleTypeDescription = Server.HtmlEncode(txtDescription.Text.Trim());
                ModuleTypeBLL bll = new ModuleTypeBLL();
                if (hfID.Value.Equals("0"))
                {
                    if (bll.InsertModuleType(info))
                    {
                        base.SetMessage(true, "保存分类成功");
                        base.Redirect("list_moduletype.aspx?action=list", 1);
                    }
                    else
                        base.SetMessage(false, "保存分类错误，请稍后重试");
                }
                else
                {
                    info.ModuleTypeID = int.Parse(hfID.Value);
                    if (bll.UpdateModuleType(info))
                    {
                        base.SetMessage(true, "修改分类成功");
                        base.Redirect("list_moduletype.aspx?action=list", 1);
                    }
                    else
                        base.SetMessage(false, "修改分类错误，请稍后重试");
                }
            }
        }
    }
}
